package assignment.ton.jenerator;
import java.util.ArrayList;
import assignment.ton.def.Class;
import assignment.ton.def.ClassLink;
import assignment.ton.def.Parameter;
import assignment.ton.def.Util;
/**
 * Main java generator
 * @author Tim
 * Generates header, calls for variables, generates constructor, calls for methods, generates footer
 */
public class Jenerator {
	private Class theClass;
	private MethJen methJen;
	private VarJen varJen;
	public Jenerator(Class c){
		theClass = c;
		methJen = new MethJen(theClass);
		varJen = new VarJen(theClass);
	}
	/**
	 * @return The main output in the form of a string array
	 */
	public String[] getClassOutput(){
		String [] output = Util.mergeStringArrays(jenerateHeader(),varJen.getOutput());
		output = Util.mergeStringArrays(output, generateLinks());
		output = Util.mergeStringArrays(output, jenerateConstructor());
		output = Util.mergeStringArrays(output, methJen.getOutput());
		output = Util.mergeStringArrays(output, jenerateFooter());
		return output;
	}
	/**
	 * @return the file header
	 */
	public String[] jenerateHeader(){
		String[] data = {"import java.util.ArrayList;","public class "+theClass.getName()+" {"};
		return data;
	}
	/**
	 * @return the files footer in string array
	 */
	public String[] jenerateFooter(){
		String[] data = {"}"};
		return data;
	}
	public String[] addTabs(String[] t){
		String[] out = new String[t.length];
		for(int i=0; i<t.length;i++){
			out[i] = "\t\t"+t[i];
		}
		return out;
	}
	/**
	 * @return the files constructor both blank and with parameters
	 */
	public String[] jenerateConstructor(){
		String[] extraCode = new String[theClass.getExtraCode().size()];
		for(int i=0; i<extraCode.length;i++){
			extraCode[i] = theClass.getExtraCode().get(i);
		}
		String[] end = {"\t}"};
		String[] data1 = {"\tpublic "+theClass.getName()+"() {"};
		data1 = Util.mergeStringArrays(data1,addTabs(extraCode));
		data1 = Util.mergeStringArrays(data1, end);
		String[] outputData = data1;
		ArrayList<Parameter> parArray = theClass.getConstructorParameters();
		String constructorItems = "";
		if(parArray.size()>0){
			for(int i = 0; i<parArray.size(); i++){
				String lastComma = "";
				if((i+1)<parArray.size()){
					lastComma = ", ";
				}
				if(parArray.get(i).isMany()){
					constructorItems = constructorItems+"ArrayList<"+parArray.get(i).getType()+"> "+parArray.get(i).getName()+lastComma;
				}else{
					constructorItems = constructorItems+parArray.get(i).getType()+" "+parArray.get(i).getName()+lastComma;
				}
			}
			String[] data2 = {"\tpublic "+theClass.getName()+"("+constructorItems+") {"};
			data2 = Util.mergeStringArrays(data2, addTabs(extraCode));
			data2 = Util.mergeStringArrays(data2, end);
			outputData = Util.mergeStringArrays(data1, data2);
		}
		return outputData;
	}
	public String[] generateLinks(){
		String[] out;
		ArrayList<ClassLink> linksList = theClass.getLinkList();
		if(linksList.size()<1){
			out = new String[0];
			return out;
		}
		out = new String[linksList.size()];
		for(int i=0; i<out.length; i++){
			ClassLink cL = linksList.get(i);
			if(cL.getMany()){
				out[i] = "\tprivate ArrayList<"+cL.getLink().getName()+"> "+cL.getLink().getName().toLowerCase()+" = new ArrayList<"+cL.getLink().getName()+">();";
			}else{
				out[i] = "\tprivate "+cL.getLink().getName()+" "+cL.getLink().getName().toLowerCase()+";";
			}
		}
		return out;
	}
		
}
